package vg.modules.graph_layout_viewer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Observable;

import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.tab.ATab;
import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventChangeTab;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.services.plugin_manager.realization.PluginParameter;
import vg.services.user_interface_manager.interfaces.AMenuItem;

public class GraphLayoutPlugin implements IPlugin {
	// Main data
	//private PluginParameter pluginParameter;
	private GraphLayoutPanel graphLayoutPanel;
	private AGraphView currGraphView;
	
	@Override
	public void install(final PluginParameter pluginParameter) throws Exception {
		//this.pluginParameter = pluginParameter;
		
		// change layout 
		this.graphLayoutPanel = new GraphLayoutPanel(pluginParameter) {
			private static final long serialVersionUID = 1L;
			
			@Override
			public void onOk() {
				execute_layout();				
			}
			
			@Override
			public void onExecute() {
				execute_layout();				
			}
		};
		JMenuItem changeLayoutMenuItem = new JMenuItem("Change layout");
        changeLayoutMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				graphLayoutPanel.open();
			}
		});
		pluginParameter.userInterface.addMenuItem(new AMenuItem(changeLayoutMenuItem) {
			public void update(Observable o, Object arg) {
				if(arg instanceof AUIEvent) {
					AUIEvent event = (AUIEvent)arg;
					switch(event.getType()) {
						case DEF_CHANGE_TAB: {
							UIEventChangeTab bufEvent = (UIEventChangeTab)event;
							ATab tab = bufEvent.getTab();
							if (tab != null) {
								switch (tab.getTabType()) {
									case ATab.DEF_GRAPH_VIEW_TAB_TYPE: {
										currGraphView = (AGraphView)tab;
										break;
									}
									default: {
										currGraphView = null;
										break;
									}
								}
							}
							break;
						}
						case DEF_CHANGE_UI_STYLE: {
							graphLayoutPanel.updateUITheme();
							break;
						}
						default: break;
					}
				}		
			}
		}, "Edit");
		graphLayoutPanel.updateUITheme();
		
		// Execute current layout
		JMenuItem executeLayoutMenuItem = new JMenuItem("Execute layout");
		executeLayoutMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				execute_layout();
			}
		});
		executeLayoutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.ALT_DOWN_MASK));
		pluginParameter.userInterface.addMenuItem(new AMenuItem(executeLayoutMenuItem) {
			public void update(Observable o, Object arg) {
			}
		}, "edit");
	}
	
//=============================================================================
//-----------------PRIVATE METHODS---------------------------------------------
	private void execute_layout() {
		if(currGraphView != null) {
			currGraphView.getHighLevelGraphEditor().executeCurrentLayout();
		}
	}
}
